<template>
  <div class="content">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button icon="el-icon-plus" plain size="mini" type="primary" @click="addRule">规则创建</el-button>
      </el-col>
    </el-row>
    <!-- <div class="defalut" v-if="ruleList.length">
      <div>
        <i class="el-icon-warning"></i>
        <span>暂无数据</span>
      </div>
    </div> -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      tooltip-effect="dark"
      style="width: 100%;">
      <el-table-column align="center" label="id" prop="id" />
      <el-table-column align="center" label="规则名称" prop="ruleName" />
      <el-table-column align="center" label="规则标识" prop="ruleIdentification" />
      <el-table-column align="center" label="规则描述" prop="remark" width="280" />
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <span style="margin-right: 10px">
            <el-tooltip class="item" content="修改" effect="light" placement="top">
              <el-button circle icon="el-icon-edit" size="mini" type="primary" @click="updateRule(scope.row)" />
            </el-tooltip>
          </span>
          <span style="margin-right: 10px">
            <el-tooltip class="item" content="删除" effect="light" placement="top">
              <el-button circle icon="el-icon-delete" size="mini" type="primary" @click="deleteRule(scope.row)" />
            </el-tooltip>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="page-box">
      <pagination :total="total" :page.sync="listQuery.page" :size.sync="listQuery.size" @pagination="getListPage" />
    </div>
    <!-- 分页 -->

    <add-event :dialog="dialog" />
    <update-event :dialog="updateDialog" />
  </div>
</template>

<script>
import { tableMixin } from '@/utils/public'
import { getListPage, deletRuleById } from '@/api/cms/monitor/event'
import addEvent from './addEvent'
import updateEvent from './updateEvent'
export default {
  name: 'Event',
  components: { addEvent, updateEvent },
  mixins: [tableMixin],
  props: {
    'productIdentification': {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 表格高度
      height: 0,
      tableKey: 0,
      list: null,
      current: 1,
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        size: 20,
        productIdentification: null
      },
      dialog: {
        title: '创建规则',
        visible: false,
        disabled: false,
        loading: false,
        ruleId: null
      },
      updateDialog: {
        title: '修改规则',
        visible: false,
        disabled: false,
        loading: false,
        id: null
      }
    }
  },
  watch: {
    productIdentification(newval, oldval) {
      if (newval !== oldval) {
        this.listQuery.productIdentification = newval
        this.getListPage()
      }
    },
    'dialog.visible'() {
      if (!this.dialog.visible) {
        this.getListPage()
      }
    },
    'updateDialog.visible'() {
      if (!this.dialog.visible) {
        this.getListPage()
      }
    }
  },
  created() {
    if (this.productIdentification) { this.listQuery.productIdentification = this.productIdentification }
    this.getListPage()
  },
  methods: {
    // 查询列表信息
    getListPage() {
      this.listLoading = true
      // 查询列表信息
      getListPage(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    addRule() {
      const dialog = {
        title: '创建规则',
        visible: true,
        disabled: false,
        loading: false,
        productIdentification: this.productIdentification
      }
      this.dialog = Object.assign(this.dialog, dialog)
    },
    updateRule: function(row) {
      this.id = row.id
      const updateDialog = {
        title: '修改规则',
        visible: true,
        disabled: false,
        loading: false,
        id: row.id
      }
      this.updateDialog = Object.assign(this.updateDialog, updateDialog)
    },
    deleteRule: function(row) {
      this.confirmBox(`确定删除这条数据?`)
        .then(() => {
          deletRuleById({ 'id': row.id })
          this.getListPage()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
  .content {
    border-radius: 0.25rem;
    border: 0.0625rem solid #EBEEF5;
    overflow: hidden;
    color: #303133;
    .mb8{
      margin: 10px 0px 20px 0px;
    }
  }
</style>
